package com.linjacy.httpproject.excel;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.util.StyleUtil;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.AbstractCellStyleStrategy;
import org.apache.poi.ss.usermodel.*;

import java.util.List;
import java.util.Map;

/**
 *  项目管理用报表导出策略
 * */
public class DrcpmReportTreeCellStyleStrategy extends AbstractCellStyleStrategy {

    boolean needTopTitle;

    private WriteCellStyle topHeaderWriteCellStyle;

    private WriteCellStyle headerWriteCellStyle;

    private WriteCellStyle subHeaderWriteCellStyle;

    private WriteCellStyle contentWriteCellStyle;

    private List<Integer> subHeadList;

    private CellStyle topHeaderCellStyle;

    private CellStyle headCellStyle;

    private CellStyle subHeadCellStyle;

    private CellStyle contentCellStyle;

    public static final int BASE_COLUMN_WIDTH = 1024;
    /**
     * 5个汉字，12个数字，10个字母
     */
    public static final int DEFAULT_COLUMN_WIDTH = BASE_COLUMN_WIDTH * 2;

    /**
     * 设置列宽
     */
    private Map<Integer,Integer> columnWidth;

    public DrcpmReportTreeCellStyleStrategy() {
    }

    public DrcpmReportTreeCellStyleStrategy(boolean needTopTitle, List<Integer> subHeadList) {
        this.needTopTitle = needTopTitle;
        this.subHeadList = subHeadList;
    }

    public DrcpmReportTreeCellStyleStrategy(boolean needTopTitle, WriteCellStyle topHeaderWriteCellStyle, WriteCellStyle headerWriteCellStyle, WriteCellStyle subHeaderWriteCellStyle, WriteCellStyle contentWriteCellStyle, List<Integer> subHeadList) {
        this.needTopTitle = needTopTitle;
        this.topHeaderWriteCellStyle = topHeaderWriteCellStyle;
        this.headerWriteCellStyle = headerWriteCellStyle;
        this.subHeaderWriteCellStyle = subHeaderWriteCellStyle;
        this.contentWriteCellStyle = contentWriteCellStyle;
        this.subHeadList = subHeadList;
    }

    public DrcpmReportTreeCellStyleStrategy(boolean needTopTitle, WriteCellStyle topHeaderWriteCellStyle, WriteCellStyle headerWriteCellStyle, WriteCellStyle subHeaderWriteCellStyle, WriteCellStyle contentWriteCellStyle, List<Integer> subHeadList,Map<Integer,Integer> columnWidth) {
        this(needTopTitle, topHeaderWriteCellStyle, headerWriteCellStyle, subHeaderWriteCellStyle, contentWriteCellStyle, subHeadList);
        this.columnWidth = columnWidth;
    }

    @Override
    protected void initCellStyle(Workbook workbook) {
        topHeaderCellStyle = StyleUtil.buildHeadCellStyle(workbook, topHeaderWriteCellStyle == null?this.getDefaultTopHeadCellStyle():topHeaderWriteCellStyle);
        headCellStyle = StyleUtil.buildHeadCellStyle(workbook, headerWriteCellStyle == null?this.getDefaultHeadCellStyle():headerWriteCellStyle);
        subHeadCellStyle = StyleUtil.buildContentCellStyle(workbook, subHeaderWriteCellStyle == null?this.getDefaultSubHeadCellStyle(): subHeaderWriteCellStyle);
        contentCellStyle = StyleUtil.buildContentCellStyle(workbook, contentWriteCellStyle == null?this.getDefaultContentCellStyle():contentWriteCellStyle);
    }

    private WriteCellStyle getDefaultContentCellStyle() {
        WriteCellStyle writeCellStyle = new WriteCellStyle();
        writeCellStyle.setBorderBottom(BorderStyle.THIN);
        writeCellStyle.setBorderRight(BorderStyle.THIN);
        writeCellStyle.setBorderLeft(BorderStyle.THIN);
        writeCellStyle.setBorderTop(BorderStyle.THIN);
        writeCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        writeCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        writeCellStyle.setFillBackgroundColor(IndexedColors.WHITE1.getIndex());
        writeCellStyle.setWrapped(true);
        WriteFont writeFont = new WriteFont();
        writeFont.setBold(false);
        writeFont.setFontHeightInPoints((short)9);
        writeCellStyle.setWriteFont(writeFont);
        return writeCellStyle;
    }

    private WriteCellStyle getDefaultSubHeadCellStyle() {
        WriteCellStyle writeCellStyle = new WriteCellStyle();
        writeCellStyle.setBorderBottom(BorderStyle.THIN);
        writeCellStyle.setBorderRight(BorderStyle.THIN);
        writeCellStyle.setBorderLeft(BorderStyle.THIN);
        writeCellStyle.setBorderTop(BorderStyle.THIN);
        writeCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        writeCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
        writeCellStyle.setFillBackgroundColor(IndexedColors.WHITE1.getIndex());
        WriteFont writeFont = new WriteFont();
        writeFont.setBold(true);
        writeFont.setFontHeightInPoints((short)10);
        writeCellStyle.setWriteFont(writeFont);
        return writeCellStyle;
    }

    private WriteCellStyle getDefaultHeadCellStyle() {
        WriteCellStyle writeCellStyle = new WriteCellStyle();
        writeCellStyle.setBorderBottom(BorderStyle.THIN);
        writeCellStyle.setBorderRight(BorderStyle.THIN);
        writeCellStyle.setBorderLeft(BorderStyle.THIN);
        writeCellStyle.setBorderTop(BorderStyle.THIN);
        writeCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        writeCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        writeCellStyle.setFillBackgroundColor(IndexedColors.WHITE1.getIndex());
        writeCellStyle.setFillPatternType(FillPatternType.NO_FILL);
        WriteFont writeFont = new WriteFont();
        writeFont.setBold(true);
        writeFont.setFontHeightInPoints((short)9);
        writeCellStyle.setWriteFont(writeFont);
        return writeCellStyle;
    }

    private WriteCellStyle getDefaultTopHeadCellStyle() {
        WriteCellStyle writeCellStyle = new WriteCellStyle();
        writeCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        writeCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        writeCellStyle.setFillBackgroundColor(IndexedColors.WHITE1.getIndex());
        writeCellStyle.setFillPatternType(FillPatternType.NO_FILL);
        WriteFont writeFont = new WriteFont();
        writeFont.setBold(true);
        writeFont.setFontHeightInPoints((short)20);
        writeCellStyle.setWriteFont(writeFont);
        return writeCellStyle;
    }



    public Map<Integer, Integer> getColumnWidth() {
        return columnWidth;
    }

    public void setColumnWidth(Map<Integer, Integer> columnWidth) {
        this.columnWidth = columnWidth;
    }

    @Override
    protected void setHeadCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
        if(needTopTitle && relativeRowIndex == 0){
            cell.getRow().setHeightInPoints((short) 26);
            cell.setCellStyle(topHeaderCellStyle);
            return;
        }
        cell.setCellStyle(headCellStyle);
    }

    @Override
    protected void setContentCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
        CellStyle contentStyle = contentCellStyle;
        if(subHeadList == null || subHeadList.size() == 0){
            cell.setCellStyle(contentStyle);
            return;
        }
        if(subHeadList.contains(relativeRowIndex)){
            contentStyle = subHeadCellStyle;
        }
        cell.setCellStyle(contentStyle);
    }

    @Override
    public void afterWorkbookDispose(WriteWorkbookHolder writeWorkbookHolder) {
        if(columnWidth == null || columnWidth.isEmpty()) {
            return ;
        }
        Workbook workbook = writeWorkbookHolder.getWorkbook();
        Sheet sheet = workbook.getSheetAt(0);
        columnWidth.forEach((col,width)->{
            sheet.setColumnWidth(col, width);
        });
    }
}

